package com.villaday.idf.fragment3;

import android.content.Intent;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.villaday.idf.R;
import com.villaday.idf.activity.CommonAddActivity;
import com.villaday.idf.adapter3.CommonAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseFragment;
import com.villaday.idf.custom.ConfirmReturnDialog;
import com.villaday.idf.custom.LoadDialog;
import com.villaday.idf.model3.GuestPostEntity;
import com.villaday.idf.model3.MemberGuestChangeEntity;
import com.villaday.idf.model3.MemberGuestEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CommonGuestFragment extends BaseFragment {

    @BindView(R.id.rv_cg)
    RecyclerView rvCg;
    @BindView(R.id.tv_cg)
    TextView tvCg;
    @BindView(R.id.tv_emptyTips)
    TextView tvEmptyTips;
    @BindView(R.id.tv_perchase)
    TextView tvPerchase;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;
    @BindView(R.id.srl_cg)
    SwipeRefreshLayout srlCg;
    @BindView(R.id.ll_cg)
    LinearLayout llCg;


    private LoadDialog loadDialog;
    private List<MemberGuestEntity.BodyBean> mBodyBean;
    private CommonAdapter commonAdapter;

    @Override
    protected int getViewResId() {
        return R.layout.fragment_common_guest;
    }

    public static CommonGuestFragment instance() {
        CommonGuestFragment commonGuestFragment = new CommonGuestFragment();
        return commonGuestFragment;
    }


    @Override
    protected void init(View v) {
        super.init(v);

        srlCg.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getGuest();
            }
        });

        initGuestRecycle();
        showDialog();
        getGuest();
    }

    private void initGuestRecycle() {
        rvCg.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        commonAdapter = new CommonAdapter(getActivity(), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                switch (v.getId()) {
                    case R.id.iv_c_retire:
                    case R.id.tv_common_more:
                    case R.id.iv_c_more:
                        Bundle bundle = new Bundle();
                        bundle.putString("data", new Gson().toJson(mBodyBean.get(position)));
                        startActivityResult(CommonAddActivity.class, Constant.Code.commonAddCode, bundle);
                        break;

//                        startActivityResult(CommonAddActivity.class, Constant.Code.commonAddCode);
//                        break;
                }
            }
        }, new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(final View v) {
                ConfirmReturnDialog confirmDialog = ConfirmReturnDialog.newInstance("温馨提示", "是否删除该旅客信息?", "我再想想", "立即删除");
                confirmDialog.setOnBtnClickListener(new ConfirmReturnDialog.OnBtnClickListener() {
                    @Override
                    public void onBtnClick(View view) {
                        int position = (int) v.getTag();
                        GuestPostEntity guestPostEntity = new GuestPostEntity();
                        guestPostEntity.setId(mBodyBean.get(position).getId());
                        guestPostEntity.setAction(2);
                        guestPostEntity.setActionType(1);
                        guestPostEntity.setMobile(mBodyBean.get(position).getMobile());
                        guestPostEntity.setEmail(mBodyBean.get(position).getEmail());
                        guestPostEntity.setIdcard(mBodyBean.get(position).getIdcard());
                        guestPostEntity.setPassport(mBodyBean.get(position).getPassport());
                        guestPostEntity.setPreferred(true);
                        Map<String, Object> map = new HashMap<>();
                        OkHttpUtil.postJson(getActivity(), Constant.URL.memberGuest, guestPostEntity, map, new OkHttpUtil.OnDataListener() {
                            @Override
                            public void onResponse(String url, String json) {
                                LogUtil.jLog().e(json);
                                if (!TextUtils.isEmpty(json)) {
                                    MemberGuestChangeEntity memberGuestChangeEntity = new Gson().fromJson(json, MemberGuestChangeEntity.class);
                                    if (memberGuestChangeEntity.getCode().equals(Constant.Strings.SUC)) {
                                        ToastUtil.initToast(getActivity(), "删除成功！");
                                        showDialog();
                                        getGuest();
                                    } else {
                                        ToastUtil.initToast(getActivity(), memberGuestChangeEntity.getMsg());
                                    }
                                }
                            }

                            @Override
                            public void onFailure(String url, String error) {

                            }
                        });
                    }

                    @Override
                    public void onDeselect(View view) {

                    }
                });
                confirmDialog.show(getActivity().getSupportFragmentManager(), "返回");
                return true;
            }
        });
        commonAdapter.setOnItemLongClickListener(new AbsRecyclerViewAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View v, final int position) {
                ConfirmReturnDialog confirmDialog = ConfirmReturnDialog.newInstance("温馨提示", "是否删除该旅客信息?", "我再想想", "立即删除");
                confirmDialog.setOnBtnClickListener(new ConfirmReturnDialog.OnBtnClickListener() {
                    @Override
                    public void onBtnClick(View view) {
                        GuestPostEntity guestPostEntity = new GuestPostEntity();
                        guestPostEntity.setId(mBodyBean.get(position).getId());
                        guestPostEntity.setAction(2);
                        guestPostEntity.setActionType(1);
                        guestPostEntity.setMobile(mBodyBean.get(position).getMobile());
                        guestPostEntity.setEmail(mBodyBean.get(position).getEmail());
                        guestPostEntity.setIdcard(mBodyBean.get(position).getIdcard());
                        guestPostEntity.setPassport(mBodyBean.get(position).getPassport());
                        guestPostEntity.setPreferred(true);
                        Map<String, Object> map = new HashMap<>();
                        OkHttpUtil.postJson(getActivity(), Constant.URL.memberGuest, guestPostEntity, map, new OkHttpUtil.OnDataListener() {
                            @Override
                            public void onResponse(String url, String json) {
                                LogUtil.jLog().e(json);
                                if (!TextUtils.isEmpty(json)) {
                                    MemberGuestChangeEntity memberGuestChangeEntity = new Gson().fromJson(json, MemberGuestChangeEntity.class);
                                    if (memberGuestChangeEntity.getCode().equals(Constant.Strings.SUC)) {
                                        ToastUtil.initToast(getActivity(), "删除成功！");
                                        showDialog();
                                        getGuest();
                                    } else {
                                        ToastUtil.initToast(getActivity(), memberGuestChangeEntity.getMsg());
                                    }
                                }
                            }

                            @Override
                            public void onFailure(String url, String error) {

                            }
                        });
                    }

                    @Override
                    public void onDeselect(View view) {

                    }
                });
                confirmDialog.show(getActivity().getSupportFragmentManager(), "返回");
                return true;
            }
        });
        rvCg.setAdapter(commonAdapter);
        commonAdapter.setEmptyView(R.layout.empty_tips);
        commonAdapter.setEmptyTips(R.id.tv_emptyTips, "--暂无相关信息--");
    }


    /**
     * 得到用户
     */
    private void getGuest() {

        Map<String, String> map = new HashMap<>();
        OkHttpUtil.getJSON(getActivity(), Constant.URL.memberGuest, map, new OkHttpUtil.OnDataListener() {
            @Override
            public void onResponse(String url, String json) {
                try {
                    LogUtil.jLog().e(json);
                    dismissLoading();
                    MemberGuestEntity memberGuestEntity = new Gson().fromJson(json, MemberGuestEntity.class);
                    if (memberGuestEntity.getCode().equals(Constant.Strings.SUC)) {
                        mBodyBean = memberGuestEntity.getBody();
                        for (int i = 0; i < mBodyBean.size(); i++) {
                            mBodyBean.get(i).setDeleted(false);
                        }
                        commonAdapter.setData(mBodyBean);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });
    }

    @OnClick({R.id.tv_cg})
    public void btnOnclick(View view) {
        switch (view.getId()) {
            case R.id.tv_cg:
                startActivityResult(CommonAddActivity.class, Constant.Code.commonAddCode);
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            showDialog();
            getGuest();
        }
    }

    private void dismissLoading() {
        if (loadDialog != null) {
            loadDialog.dismiss();//获取信息后关闭对话框
        }

        if (srlCg != null && srlCg.isRefreshing()) {
            srlCg.setRefreshing(false);
        }
    }

    protected void showDialog() {
        if (loadDialog == null) {
            loadDialog = LoadDialog.newInstance("加载中");
        }
        loadDialog.show(getActivity().getSupportFragmentManager(), "loading");
    }
}
